package gui.panels.play;

import gui.models.PlayModel;
import gui.utils.ComponentUtils;

import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;

public class PlayQuestionsPanel extends JLayeredPane {
    
    private static final long serialVersionUID = 1L;

    private SpeechPanel speechPanel;
	private JLabel akinatorLabel;
	private final PlayModel model;
    
    public PlayQuestionsPanel(PlayModel model) {
    	this.model = model;
		buildPane();
    }

	private void buildPane() {
		setPreferredSize(new Dimension(500, 400));
		setBounds(0, 0, 500, 400);
		
		akinatorLabel = new JLabel(AkinatorStateEnum.INTRO.readImageIcon());
		akinatorLabel.setBounds(80, 110, 600, 500);
		akinatorLabel.setOpaque(false);

		String initText = "Hello, I am Fictionator.\nChoose a fictional character and press Start when you are ready.";
		speechPanel = new SpeechPanel(initText);
		speechPanel.setBounds(120, 10, 300, 300);
		speechPanel.setOpaque(true);
		
		add(speechPanel, new Integer(0), 0);
		add(akinatorLabel, new Integer(1), 0);
	}
	
	public void update() {
		ImageIcon imageIcon = null;
		if (model.isGuessed() && model.isSubmitedGuess()) {
			if (model.isCorrectGuess()) {
				imageIcon = AkinatorStateEnum.WIN.readImageIcon();
				speechPanel.changeText("Yes!!! I am the greatest");
			} else {
				imageIcon = AkinatorStateEnum.LOSE.readImageIcon();
				speechPanel.changeText("I am an idiot");
			}
		} else if (model.isGuessed()) {
			imageIcon = AkinatorStateEnum.GUESS.readImageIcon();
			speechPanel.changeText("You chose: " + model.getGuess());
		} else if (model.getAnswer() == null) {
			imageIcon = AkinatorStateEnum.CALM.readImageIcon();
			speechPanel.changeText(model.getNextQuestion().getLeft() + ". " + model.getNextQuestion().getRight());
		} else {
			switch (model.getAnswer()) {
			case YES:
				imageIcon = AkinatorStateEnum.HAPPY.readNextImageIcon();
				break;
			case NO:
				imageIcon = AkinatorStateEnum.ANGRY.readNextImageIcon();
				break;
			case DONT_KNOW:
				imageIcon = AkinatorStateEnum.CALM.readNextImageIcon();
				break;
			}
			speechPanel.changeText(model.getNextQuestion().getLeft() + ". " + model.getNextQuestion().getRight());
		}
		akinatorLabel.setIcon(imageIcon);
	}
	
	enum AkinatorStateEnum {
		INTRO("intro",1),
		CALM("calm",3),
		HAPPY("happy",2),
		ANGRY("angry",3),
		WIN("win",1),
		LOSE("lose",1),
		GUESS("calm_3",1);
		
		private final String stateName;
		private final int numOfPictures;
		private static AkinatorStateEnum lastState = AkinatorStateEnum.INTRO;
		private static int lastPicturesNumber = 1;
		
		AkinatorStateEnum(String stateName, int numOfPictures) {
			this.stateName = stateName;
			this.numOfPictures = numOfPictures;	
		}
		
		private boolean isSinglePicture() {
			return numOfPictures == 1;
		}
		
		private ImageIcon readImageIcon() {
			return readImageIcon(1);
		}
		
		private ImageIcon readImageIcon(int pictureNumber) {
			String fileName;
			if (isSinglePicture()) {
				fileName = "akinator_" + stateName + ".png";
			} else {
				fileName = "akinator_" + stateName + "_" + pictureNumber + ".png";
			}	
			lastState = this;
			lastPicturesNumber = pictureNumber;
			return ComponentUtils.readScaledImageIcon(fileName, 320, 313);	
		}
		
		private ImageIcon readNextImageIcon() {
			if (lastState.equals(this)) {
				int nextPictureNumber = lastPicturesNumber + 1;
				nextPictureNumber -= nextPictureNumber > numOfPictures ? numOfPictures : 0;
				return readImageIcon(nextPictureNumber);
			}
			return readImageIcon();
		}
	}
	
}
